package cn.tnar.controller;

import cn.tnar.annotation.Token;
import cn.tnar.constant.ResultCode;
import cn.tnar.model.entity.TAcctCustLoginInfo;
import cn.tnar.model.service.ITAcctCustLoginInfoService;
import cn.tnar.request.ConditionRequest;
import cn.tnar.request.LoginRequest;
import cn.tnar.response.CustLoginInfo;
import cn.tnar.response.Result;
import cn.tnar.utils.JwtUtil;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @Author: 陈亮
 * @Date: 2018/10/12 16:20
 */
@Api(tags = "账号信息表(陈亮) t_acct_cust_login_info")
@RestController
@RequestMapping("/tAcctCustLoginInfo")
public class TAcctCustLoginInfoController {

    private Result result = new Result();

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @Autowired
    private ITAcctCustLoginInfoService it_acct_cust_login_infoService;


    @ApiOperation(value = "登录 - t_acct_cust_login_info")
    @PostMapping(value = "login")
    public Result<TAcctCustLoginInfo> login(@RequestBody LoginRequest request) {
        Wrapper<TAcctCustLoginInfo> wrapper = new EntityWrapper<TAcctCustLoginInfo>();
        //账号
        wrapper.eq("opername", request.getOpername());
        //密码
        wrapper.eq("trade_pwd", request.getTrade_pwd());
        TAcctCustLoginInfo tAcctCustLoginInfo = it_acct_cust_login_infoService.login(request, wrapper);
        if (tAcctCustLoginInfo != null) {
            CustLoginInfo custLoginInfo = new CustLoginInfo();
            BeanUtils.copyProperties(tAcctCustLoginInfo, custLoginInfo);

//TODO重复登录问题处理

            String token = JwtUtil.createJWT(custLoginInfo.getCustId().toString(), custLoginInfo.getOpername());
            custLoginInfo.setToken(token);
            redisTemplate.opsForValue().set(token, custLoginInfo, 1, TimeUnit.HOURS);
            result.setData(custLoginInfo);
        } else {
            result.setData(null);
        }
        result.setResultCode(ResultCode.SUCCESS);
        return result;
    }

    @Token
    @ApiOperation(value = "查询所有 - t_acct_cust_login_info")
    @PostMapping(value = "query")
    public Result<TAcctCustLoginInfo> query(@RequestHeader("Token") String token, @RequestBody ConditionRequest request) {
        Map<Object, Object> conditionMap = request.getConditionMap();
        Wrapper<TAcctCustLoginInfo> condition = new EntityWrapper<TAcctCustLoginInfo>();
        Page<TAcctCustLoginInfo> page = it_acct_cust_login_infoService.selectListPage(request.getCurrent(), request.getSize());
        Page<TAcctCustLoginInfo> list = it_acct_cust_login_infoService.selectPage(page, condition);
        if (list != null && list.getSize() > 0) {
            result.setData(list);
        } else {
            result.setData(null);
        }
        result.setResultCode(ResultCode.SUCCESS);
        return result;
    }
}
